import { constantRoutes } from '@/router';
import { store } from '@/store';
import { listRoutes } from '@/api/system/menu/index';

const modules = import.meta.glob('../../views/**/**.vue');
const Layout = () => import('@/layout/index.vue');

// setup
export const usePermissionStore = defineStore('permission', () => {
  // state
  const routes = ref([]);

  // actions
  function setRoutes(newRoutes) {
    routes.value = constantRoutes.concat(newRoutes);
  }
  /**
   * 生成动态路由
   *
   * @returns
   */
  function generateRoutes() {
    return new Promise((resolve, reject) => {
      // 接口获取所有路由
      listRoutes()
        .then(({ data: asyncRoutes }) => {
          // 根据角色获取有访问权限的路由
          const accessedRoutes = filterAsyncRoutes(asyncRoutes);
          setRoutes(accessedRoutes);
          resolve(accessedRoutes);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  /**
   * 递归过滤有权限的异步(动态)路由
   *
   * @param routes 接口返回的异步(动态)路由
   * @param roles 用户角色集合
   * @returns 返回用户有权限的异步(动态)路由
   */
  const filterAsyncRoutes = (routes) => {
    const asyncRoutes = [];

    routes.forEach((route) => {
      const tmpRoute = { ...route }; // ES6扩展运算符复制新对象
      if (!route.name) {
        tmpRoute.name = route.path;
      }
      // 判断用户(角色)是否有该路由的访问权限
      if (tmpRoute.component?.toString() == 'Layout') {
        tmpRoute.component = Layout;
      } else {
        const component = modules[`../../views/${tmpRoute.component}.vue`];
        if (component) {
          tmpRoute.component = component;
        } else {
          tmpRoute.component = modules[`../../views/error-page/404.vue`];
        }
      }

      if (tmpRoute.children) {
        tmpRoute.children = filterAsyncRoutes(tmpRoute.children);
      }

      asyncRoutes.push(tmpRoute);
    });

    return asyncRoutes;
  };
  /**
   * 获取与激活的顶部菜单项相关的混合模式左侧菜单集合
   */
  const mixLeftMenus = ref([]);
  function setMixLeftMenus(topMenuPath) {
    const matchedItem = routes.value.find((item) => item.path === topMenuPath);
    if (matchedItem && matchedItem.children) {
      mixLeftMenus.value = matchedItem.children;
    }
  }
  return {
    routes,
    setRoutes,
    generateRoutes,
    mixLeftMenus,
    setMixLeftMenus,
  };
});

// 非setup
export function usePermissionStoreHook() {
  return usePermissionStore(store);
}
